package two;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ExerciseV2 {
    private List<AbstractExpression> exercises;
    private int count;
    private Random random = new Random();

    // 构造方法：生成指定数量和类型的习题
    public ExerciseV2(int count, String type) {
        this.count = count;
        this.exercises = new ArrayList<>();
        generateExercises(type);
    }

    // 生成习题逻辑
    private void generateExercises(String type) {
        for (int i = 0; i < count; i++) {
            int num1 = random.nextInt(100) + 1;
            int num2 = random.nextInt(100) + 1;
            switch (type.toLowerCase()) {
                case "add":
                    exercises.add(new AddExpression(num1, num2));
                    break;
                case "subtract":
                    exercises.add(new SubtractExpression(num1, num2));
                    break;
                case "mix":
                    // 混合题型：随机选择加减
                    if (random.nextBoolean()) {
                        exercises.add(new AddExpression(num1, num2));
                    } else {
                        exercises.add(new SubtractExpression(num1, num2));
                    }
                    break;
                default:
                    throw new IllegalArgumentException("不支持的题型：" + type);
            }
        }
    }

    // 格式化打印（带序号，每行5道题）
    public void printFormatted() {
        System.out.println("===== 算术习题（共" + count + "道）=====");
        for (int i = 0; i < exercises.size(); i++) {
            System.out.printf("%2d. %-12s", (i + 1), exercises.get(i));
            if ((i + 1) % 5 == 0) System.out.println(); // 每5道换行
        }
        if (count % 5 != 0) System.out.println(); // 最后一行补换行
    }

    // 供测试用：获取习题列表
    public List<AbstractExpression> getExercises() {
        return exercises;
    }
}